const app = getApp()
var countdown = 60;
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: true
    })
    countdown = 60;
    return;
  } else {
    that.setData({
      is_show: false,
      last_time: countdown
    })

    countdown--;
  }
  setTimeout(function () {
    settime(that)
  }
    , 1000)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    code: '',
    last_time: '',
    is_show: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  phone: function(e) {
    this.setData({
      phone: e.detail.value
    });
    app.globalData.phone = e.detail.value;
    console.log('app.globalData.phone1: ' + app.globalData.phone)
  },

  code: function(e) {
    this.setData({
      code: e.detail.value
    });
  },

  getAuthCode: function() {
    if (this.data.phone == '') {
      wx.showToast({
        title: '请先输入手机号码',
        icon: 'none',
        duration: 2000
      });
    } else {
      var that = this;
      // 将获取验证码按钮隐藏60s，60s后再次显示
      that.setData({
        is_show: (!that.data.is_show)  //false
      })
      settime(that);
      wx.request({
        url: 'http://' + app.globalData.host + '/register',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
        },
        data: {
          phone: this.data.phone
        },
        success: function(res) {
          if (res.data.code == 1) {
            wx.showToast({
              title: '发送成功',
              icon: 'success',
              duration: 2000
            });
            app.globalData.sessionId = res.data.data.sessionId;
          } else {
            wx.showToast({
              title: '发送失败',
              icon: 'none',
              duration: 2000
            });
          }
        },
        fail: function() {
          wx.showToast({
            title: '网络连接出错，请重试！',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }

  },
  checkregister: function() {
    if (this.data.code == '' || this.data.phone == '') {
      wx.showToast({
        title: '请先输入手机号码和验证码',
        icon: 'none',
        duration: 2000
      })
    } else {
      console.log('sessionId is : ' + this.data.sessionId)
      wx.request({
        url: 'http://' + app.globalData.host + '/check/register',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
        },
        data: {
          authCode: this.data.code,
          phone: this.data.phone
        },
        success: function(res) {
          if (res.data.code == 1) {
            wx.showToast({
              title: '验证成功',
              icon: 'success',
              duration: 2000
            });
            wx.redirectTo({
              url: '/pages/user/password/password',
            })
          } else {
            wx.showToast({
              title: '验证码不正确',
              icon: 'none',
              duration: 2000
            })
            console.log("验证码发送失败")
          }
        },
        fail: function() {
          wx.showToast({
            title: '验证码发送出错！',
            icon: 'none',
            duration: 2000
          })
          console.log("验证码发送出错！")
        }
      })
    }
  }
})